package com.ohs.bll.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.ohs.bll.service.ExportService;
import com.ohs.bll.util.DateUtil;
import com.ohs.dal.model.Booking;

public class ExcelServiceImpl implements ExportService{

	public void export(HttpServletResponse response, List<Booking> bookings, String selectedDate, String roomName)
			throws Exception {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet();
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue(roomName);
		HSSFRow row2 = sheet.createRow(1);
		HSSFCell cellDate = row2.createCell(0);
		cellDate.setCellValue(selectedDate);
		HSSFRow rowHeader = sheet.createRow(2);
		HSSFCell cellHeader = rowHeader.createCell(0);
		cellHeader.setCellValue("Ngày giờ hẹn");
		HSSFCell cellHeader1 = rowHeader.createCell(1);
		cellHeader1.setCellValue("Tên bệnh nhân");
		HSSFCell cellHeader2 = rowHeader.createCell(2);
		cellHeader2.setCellValue("Tuổi");
		HSSFCell cellHeader3 = rowHeader.createCell(3);
		cellHeader3.setCellValue("Điện thoại");
		HSSFCell cellHeader4 = rowHeader.createCell(4);
		cellHeader4.setCellValue("Địa chỉ");
		HSSFCell cellHeader5 = rowHeader.createCell(5);
		cellHeader5.setCellValue("Tái khám");
		HSSFCell cellHeader6 = rowHeader.createCell(6);
		cellHeader6.setCellValue("Triệu chứng");
		int rowCount = 3;
		for (Booking booking : bookings) {
			HSSFRow aRow = sheet.createRow(rowCount);
			HSSFCell cell1 = aRow.createCell(0);
			cell1.setCellValue(DateUtil.dateToString(booking.getBookingDate()));
			HSSFCell cell2 = aRow.createCell(1);
			cell2.setCellValue(booking.getName());
			HSSFCell cell3 = aRow.createCell(2);
			cell3.setCellValue(booking.getAge());
			HSSFCell cell4 = aRow.createCell(3);
			cell4.setCellValue(booking.getPhone());
			HSSFCell cell5 = aRow.createCell(4);
			cell5.setCellValue(booking.getAddress());
			HSSFCell cell6 = aRow.createCell(5);
			String recheck = (booking.isRecheck()) ? "tái khám" : "";
			cell6.setCellValue(recheck);
			HSSFCell cell7 = aRow.createCell(6);
			cell7.setCellValue(booking.getDescription());
			rowCount++;
		}

		// write it as an excel attachment
		ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
		wb.write(outByteStream);
		byte [] outArray = outByteStream.toByteArray();
		response.setContentType("application/octet-stream");
		response.setContentLength(outArray.length);
		response.setHeader("Content-Disposition", "attachment; filename=export_" + Calendar.getInstance().getTimeInMillis() + ".xls");
		OutputStream outStream = response.getOutputStream();
		outStream.write(outArray);
		outStream.flush();
	}
	
}
